﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ResxCrunch.Utils;
using ResxCrunch.Business;
using ResxCrunch.Business.Managers;

namespace ResxCrunch
{
    /// <summary>
    /// Select keys form class
    /// </summary>
    public partial class FrmSelectKeys : Form
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="FrmSelectKeys"/> class.
        /// </summary>
        public FrmSelectKeys()
        {
            InitializeComponent();
        }

        #region Properties
        /// <summary>
        /// Gets or sets the file table.
        /// </summary>
        /// <value>The file table.</value>
        public DataTable KeysTable
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the selected files.
        /// </summary>
        /// <value>The selected files.</value>
        public List<string> SelectedKeys
        {
            get;
            set;
        }

        /// <summary>
        /// Sets a value indicating whether this <see cref="FrmMain"/> is busy.
        /// </summary>
        /// <value><c>true</c> if busy; otherwise, <c>false</c>.</value>
        public bool Busy
        {
            set
            {
                if (value)
                {
                    lblBusy.Visible = true;
                    this.Refresh();
                }
                else
                {
                    lblBusy.Visible = false;
                }
            }
        }
        #endregion Properties

        /// <summary>
        /// Handles the Load event of the FrmKeysSelect control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void FrmKeysSelect_Load(object sender, EventArgs e)
        {
            this.LoadKeys();
            this.UpdateValues();
        }

        /// <summary>
        /// Updates the values.
        /// </summary>
        private void UpdateValues()
        {
            lblNrSelected.Text = string.Concat(lstKeys.SelectedItems.Count, "/", lstKeys.Items.Count, " selected");
        }

        /// <summary>
        /// Loads the files from the datatable
        /// </summary>
        private void LoadKeys()
        {
            lstKeys.Items.Clear();

            List<string> items = new List<string>();

            if (this.KeysTable != null && this.KeysTable.Rows.Count > 0 && this.KeysTable.Columns.Contains(Enums.DataColumn.Key))
            {
                string newKey = string.Empty;
                foreach (DataRow dr in this.KeysTable.Rows)
                {
                    newKey = ResxMngr.Instance.GetKey(dr[Enums.DataColumn.Key].ToString());

                    if (newKey.Length > 0 && !items.Contains(newKey))
                    {
                        items.Add(newKey);
                    }
                }
            }

            items.Sort();

            foreach (string item in items)
            {
                lstKeys.Items.Add(item);
            }
        }               

        /// <summary>
        /// Handles the Click event of the btnOk control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnOk_Click(object sender, EventArgs e)
        {
            List<string> selectedKeyList = new List<string>();

            foreach (object item in lstKeys.SelectedItems)
            {
                selectedKeyList.Add(item.ToString());
            }

            this.SelectedKeys = selectedKeyList;
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        /// <summary>
        /// Handles the SelectedValueChanged event of the lstKeys control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void lstKeys_SelectedValueChanged(object sender, EventArgs e)
        {
            this.UpdateValues();
        }
    }
}
